/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.sms.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.google.code.sms.Logger;
import com.google.code.sms.domain.MediaElement;
import com.google.code.sms.service.MediaElementService;
import com.google.code.sms.service.SecurityService;
import com.google.code.sms.util.FileUtil;
import com.google.code.sms.util.StringUtil;

/**
 * Controller which provides subtitles.
 * 
 * @author Scott Ware
 */
public class SubtitlesController implements Controller {
        
    private static final Logger LOG = Logger.getLogger("SubtitlesController");

    private SecurityService securityService;
    private MediaElementService mediaElementService;
    
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        File file = getSubtitlesFile(request);
        
        if(file == null)
        {
            return null;
        }
        
        // Check subtitles exist
        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }

        // Check access.
        if (!securityService.isReadAllowed(file)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return null;
        }

        // Send subtitles
        sendSubtitles(file, response);
        return null;
    }
    
    private File getSubtitlesFile(HttpServletRequest request) {
        
        String id = request.getParameter("id");
                
        if (id != null)
        {
            MediaElement mediaElement = mediaElementService.getMediaElement(Integer.valueOf(id));
            return mediaElement == null ? null : mediaElementService.getSubtitles(mediaElement);
        }

        String path = StringUtils.trimToNull(request.getParameter("path"));
        return path != null ? new File(path) : null;
    }

    private void sendSubtitles(File file, HttpServletResponse response) throws IOException 
    {
        response.setContentType(StringUtil.getMimeType(FilenameUtils.getExtension(file.getName())));
        InputStream in = null;
        
        try {
            in = new FileInputStream(file);
            IOUtils.copy(in, response.getOutputStream());
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    
    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

    public void setMediaElementService(MediaElementService mediaElementService) {
        this.mediaElementService = mediaElementService;
    }
}
